package com.example.mybatisusers.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author 27013
 **/


public class MyBatisUtil {
    private static SqlSession sqlSession;

    public static SqlSession createSession() throws IOException {
        //初始化工厂建造者
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        //将核心配置文件转化成input Stream流
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory factory=sqlSessionFactoryBuilder.build(inputStream);
        // 通过SqlSessionFactory生产出SqlSession对象 --会话 在完成操作后，必须释放
        //参数为true表示自动提交
        sqlSession=factory.openSession(true);
        return sqlSession;
    }
}
